//
//  MainViewController.m
//  WWAVPlayer
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 LZY. All rights reserved.
//
#import "MainViewController.h"
#import "ListViewController.h"
#import "PlayDetailViewController.h"
#import "RecommendCollectionViewCell.h"
#import "ButtonCollectionViewCell.h"
#import "ShowMoreCollectionViewCell.h"
#import "Blocks.h"
#import "Contents.h"
#import "Show_Button_ViewController.h"
#import "PlayDetailViewController.h"
#import "ListViewController.h"
#import <MJRefresh.h>
#define MeanuHeight 40 * H
@interface MainViewController ()<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,ShowButtonDelegate,ButtonCollectionViewCellDelegate,RecommendCollectionViewCellDelegate,RecommendCollectionViewCellDelegate,ButtonCollectionViewCellDelegate>
@property (nonatomic,strong)NSMutableArray *buttonArr;
@property (nonatomic,strong)NSArray *buttonNameArr;
@property (nonatomic,strong)NSArray *idArr;
@property (nonatomic,strong)UIView *view1;
@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,assign)CGFloat buttonWidth;
@property (nonatomic,strong)NSMutableArray *dataArr;

@end

@implementation MainViewController

- (void)passToMainControllerWithBlock:(Blocks *)block
{
    
    ListViewController *listVC = [[ListViewController alloc] init];
    
    listVC.block = block;
    
    [self.navigationController pushViewController:listVC animated:NO];
    
}
- (void)passToMainControllerWithIdNum:(NSString *)idNum
{
    PlayDetailViewController *detailVC = [[PlayDetailViewController alloc] init];
    
    [self.navigationController pushViewController:detailVC animated:NO];
    
}
/**** PZ查看全部button的点击方法 ********/
- (void)passindex:(NSInteger)index
{
    NSLog(@"%ld",index);
    Show_Button_ViewController *showButtonVC = [[Show_Button_ViewController alloc]init];
    showButtonVC.channelNum = index;
    [self.navigationController pushViewController:showButtonVC animated:YES];
    
}
/******************************************************/

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr = [NSMutableArray array];
    //整合用::::::
    /******************************************************/
    self.title = @"首页";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(rightButtonAction:)];
                                              
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(leftButtonAction:)];
    /******************************************************/
    self.idArr = [NSArray arrayWithObjects:@"1",@"10",@"3",@"8",@"4",@"17",@"6",@"11",@"5", nil];

    
    [self setCollectionViewAndScrollecView];
    
    [self handleDataWithIdNum:0];
    
    // Do any additional setup after loading the view.
}
//铺底层框架  滚动标题和滚动视图
- (void)setCollectionViewAndScrollecView
{
    
    self.buttonWidth = self.view.bounds.size.width / 4;
    
    self.buttonNameArr = @[@"推荐",@"搞笑",@"电影",@"娱乐",@"电视剧",@"体育",@"综艺",@"美女",@"动漫"];
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, 375, MeanuHeight)];
    _scrollView.backgroundColor = [UIColor colorWithWhite:0.906 alpha:1.000];
    
    _scrollView.showsHorizontalScrollIndicator = NO;
    self.buttonArr = [NSMutableArray array];
    
    _scrollView.contentSize = CGSizeMake(self.buttonNameArr.count * self.buttonWidth, -64);
    self.scrollView.contentOffset = CGPointMake(0, 0);
    [self.view addSubview:_scrollView];
    
    self.view1 = [[UIView alloc]init];
    self.view1.frame = CGRectMake(0, MeanuHeight - 2- 64, self.buttonWidth, 2);
    self.view1.backgroundColor = [UIColor orangeColor];
    [_scrollView addSubview:self.view1];
    
    for (int i = 0; i <  self.buttonNameArr.count ; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0 + i * self.buttonWidth , -64, self.buttonWidth , MeanuHeight - 2);
        button.tag = 1000 + i;
        
        [button setTitle:self.buttonNameArr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont fontWithName:@"Verdana-Bold" size:15]];
        [button addTarget:self action:@selector(butClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonArr addObject:button];
        [_scrollView addSubview:button];
        if (button.tag == 1000) {
            //            button.backgroundColor = [UIColor colorWithRed:0.2363 green:0.7842 blue:0.1279 alpha:1.0];
            [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        }else{
            //            button.backgroundColor = [UIColor colorWithRed:0.14 green:0.8012 blue:0.8338 alpha:0.24]
            //            ;
        }
    }
    
    UICollectionViewFlowLayout *flowayout = [[UICollectionViewFlowLayout alloc]init];
    
    //每个cell的大小
    flowayout.itemSize = CGSizeMake(375,667 - MeanuHeight - 64);
    
    flowayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //collectionView的滚动方向
    flowayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //行间距
    
    flowayout.minimumLineSpacing = 0;
    //每一行两个cell 距离
    flowayout.minimumInteritemSpacing = 0;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, MeanuHeight + 2 + 64 , 375, 667 - MeanuHeight - 64) collectionViewLayout:flowayout];
    [self.view addSubview:self.collectionView];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    self.collectionView.pagingEnabled = YES;
    //1.初始化一个collectionview必须要给定一个布局对象
    
    //UICollectionViewLayout是一个抽象类
    
    //UICollectionViewFlowLayout是苹果写好的一个布局类,用于标准的瀑布流布局
    
    //2.collectionview必须在创建的时候就要注册要使用cell
    
    [self.collectionView registerClass:[RecommendCollectionViewCell class] forCellWithReuseIdentifier:@"recommend"];
    [self.collectionView registerClass:[ShowMoreCollectionViewCell class] forCellWithReuseIdentifier:@"showMore"];
    [self.collectionView registerClass:[ButtonCollectionViewCell class] forCellWithReuseIdentifier:@"button"];
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.buttonNameArr.count ;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {

        RecommendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"recommend" forIndexPath:indexPath];
        [self RefreshCellTableView:cell IdNum:indexPath.row];
        cell.dataArr = [NSMutableArray array];
        for (Blocks *tempBlock in self.dataArr) {
            if ([tempBlock.id isEqualToString:@"296"] || [tempBlock.id isEqualToString:@"24"] || [tempBlock.id isEqualToString:@"397"] || [tempBlock.id isEqualToString:@"10"] || [tempBlock.id isEqualToString:@"300"] || [tempBlock.id isEqualToString:@"11"] || [tempBlock.id isEqualToString:@"12"] || [tempBlock.id isEqualToString:@"14"] || [tempBlock.id isEqualToString:@"13"] ){
    
                [cell.dataArr addObject:tempBlock];
            }
        }
        cell.delegate = self;
//        [self RefreshCellTableView:cell IdNum:indexPath.row];
        [cell.tableView reloadData];
        cell.contentView.backgroundColor = [UIColor grayColor];
        return cell;
    }else if (indexPath.row == 1 ){
    
        ShowMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"showMore" forIndexPath:indexPath];
        cell.dataArr = self.dataArr;
        cell.delegate = self;
        cell.channel = 1004;
        [self RefreshCellTableView:cell IdNum:indexPath.row];
        [cell.tableView reloadData];
        cell.contentView.backgroundColor = [UIColor grayColor];
        return cell;
    }else if (indexPath.row == 2 ){
        
        ButtonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"button" forIndexPath:indexPath];
        cell.dataArr = self.dataArr;
        cell.delegate = self;
        [self RefreshCellTableView:cell IdNum:indexPath.row];
        [cell.tableView reloadData];
        cell.contentView.backgroundColor = [UIColor grayColor];
        return cell;
    }else if (indexPath.row == 3 ){
        
        ShowMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"showMore" forIndexPath:indexPath];
        cell.dataArr = self.dataArr;
        cell.delegate = self;
        cell.channel = 1001;
        [self RefreshCellTableView:cell IdNum:indexPath.row];
        [cell.tableView reloadData];
        cell.contentView.backgroundColor = [UIColor grayColor];
        return cell;
    }else if (indexPath.row == 4 ){
        
        ButtonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"button" forIndexPath:indexPath];
        cell.dataArr = self.dataArr;
        cell.delegate = self;
        [self RefreshCellTableView:cell IdNum:indexPath.row];
        [cell.tableView reloadData];
        
        cell.contentView.backgroundColor = [UIColor grayColor];
        return cell;
    }else if (indexPath.row == 5 ){
        
        ShowMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"showMore" forIndexPath:indexPath];
        cell.dataArr = self.dataArr;
        cell.delegate = self;
        cell.channel = 1002;
        [self RefreshCellTableView:cell IdNum:indexPath.row];
        [cell.tableView reloadData];
        cell.contentView.backgroundColor = [UIColor grayColor];
        return cell;
    }else if (indexPath.row == 6 ){
        
        ButtonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"button" forIndexPath:indexPath];
        cell.dataArr = self.dataArr;
        cell.delegate = self;
        [self RefreshCellTableView:cell IdNum:indexPath.row];
         [cell.tableView reloadData];
        cell.contentView.backgroundColor = [UIColor grayColor];
        return cell;
    }else if (indexPath.row == 7 ){
        
        ShowMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"showMore" forIndexPath:indexPath];
        cell.dataArr = self.dataArr;
        cell.delegate = self;
        cell.channel = 1013;

        [self RefreshCellTableView:cell IdNum:indexPath.row];
        [cell.tableView reloadData];
        cell.contentView.backgroundColor = [UIColor grayColor];
        return cell;
    }else {
        
        ButtonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"button" forIndexPath:indexPath];
        cell.dataArr = self.dataArr;
        cell.delegate = self;
        [self RefreshCellTableView:cell IdNum:indexPath.row];
         [cell.tableView reloadData];
        cell.contentView.backgroundColor = [UIColor grayColor];
        return cell;
    }
}
- (void)RefreshCellTableView:(BaseCollectionViewCell *)cell IdNum:(NSInteger)idNum;
{
    //下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //延迟2秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.dataArr = [NSMutableArray array];
            [self handleDataWithIdNum:idNum];
            [cell.tableView reloadData];
            [cell.tableView.header endRefreshing];
            
        });
    }];
    //设置刷新控件
    cell.tableView.header = header;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{
    NSInteger i = self.collectionView.contentOffset.x / 375;
   
    UIButton *button = [self.buttonArr objectAtIndex:i];
  
    [self butClicked:button];
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

}

-(void)butClicked:(UIButton *)button
{
    NSInteger i = button.tag - 1000;

    self.view1.frame = CGRectMake(self.buttonWidth * i, MeanuHeight - 2 - 64, self.buttonWidth, 2);
    [self.collectionView setContentOffset:CGPointMake(375 * i, 0) animated:YES];
    for (UIButton *button in self.self.buttonArr) {

        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
    
    int num =  [UIScreen mainScreen].bounds.size.width / self.buttonWidth;
    if (i >= num - 2 && i <= self.buttonNameArr.count - num +2) {
        [self.scrollView setContentOffset: CGPointMake(i * self.buttonWidth - (num-2)*self.buttonWidth , -64)animated:YES];
    }
    
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

    self.dataArr = [NSMutableArray array];
    
    [self handleDataWithIdNum:i];
    
    [self.collectionView reloadData];
    
}

/******************************************************************/
//数据请求
- (void)handleDataWithIdNum:(NSInteger)idNum
{
    NSString *str = [NSString stringWithFormat:@"http://po.funshion.com/v5/config/channelhome?nav_id=%@&cl=aphone&ve=2.4.2.3&mac=f48b320f56d1&uc=6",[self.idArr objectAtIndex:idNum]];
    [NetWorkManager GET:str isCaChe:YES resultData:^(id responseObject) {
        
        NSDictionary *dic = responseObject;
        
        NSArray *arr = [dic objectForKey:@"blocks"];
        
        for (NSDictionary *tempDic in arr) {
            Blocks *block = [[Blocks alloc] init];
            
            block.contents = [NSMutableArray array];
            
            [block setValuesForKeysWithDictionary:tempDic];
            
            [self.dataArr addObject:block];
           
            [self.collectionView reloadData];
        }
    
    } resultRrror:^(NSError *error) {
        
    }];
    
}



/******************************************************************/
//整合用::::+
/******************************************************/
- (void)rightButtonAction:(UIButton *)btn
{
    ListViewController *listVC = [[ListViewController alloc] init];
    
    [self.navigationController pushViewController:listVC animated:YES];
}

- (void)leftButtonAction:(UIButton *)btn
{
    PlayDetailViewController *playDeVC = [[PlayDetailViewController alloc] init];
    
    [self.navigationController pushViewController:playDeVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
